from PyQt5.QtWidgets import QWidget, QApplication, QFileDialog
from PyQt5.QtGui import QPixmap
import sys
import os
import cv2

class win(QWidget):
    def __init__(self):
        super().__init__()
        #self.resize(300,300)

        from PyQt5.uic import loadUi  # 需要导入的模块
        loadUi("viewer.ui", self)  #加载UI文件

        self.img_path = os.getcwd()
        self.curr_img_id = 0
        self.genImgNames()
        self.genImgInfos()
        self.view()

        self.btnImgPath.clicked.connect(self.selectImgPath)
        self.btnPrev.clicked.connect(self.prev)
        self.btnNext.clicked.connect(self.next)

    def genImgNames(self):
        self.img_names = []
        try:
            self.img_names = [f[:-9] for f in os.listdir(self.img_path) if f[-9:].lower() == 'cntnt.jpg']
            if len(self.img_names) > 0:
                self.curr_img_id = 0
        except Exception as e:
            self.lbInfo.setText(e)

    def selectImgPath(self):
        try:
            self.curr_img_id = -1
            self.img_path = QFileDialog.getExistingDirectory(self, 'Select Path', os.getcwd())
            self.txtImgPath.setText(self.img_path)
            self.genImgNames()
            self.genImgInfos()
            self.view()
        except Exception as e:
            self.lbInfo.setText(e)

    def view(self):
        if self.curr_img_id >= 0:
            try:
                name = self.img_names[self.curr_img_id]
                src_img = QPixmap(os.path.join(self.img_path, name+'cntnt.JPG'))
                self.lbSrcImg.setPixmap(src_img)
                mask = QPixmap(os.path.join(self.img_path, name+'cntnt_mask.JPG'))
                self.lbMaskImg.setPixmap(mask)
                self.lbInfo.setText('id: {}\nexcellent_rate: {}\ntotal: {}\nindex: {}\ninside: {:.4f}\noutside: {:.4f}'.format(name, self.excellent_rate, len(self.img_names), self.curr_img_id + 1, self.img_infos[name][0], self.img_infos[name][1]))
            except Exception as e:
                self.lbInfo.setText(e)

    def calcExcellentRate(self):
        inside_excellent_count = 0
        for v in self.img_infos.values():
            if v[0] < 0.7:
                inside_excellent_count += 1
        self.excellent_rate = inside_excellent_count / len(self.img_infos)

    def genImgInfos(self):
        self.img_infos = {}
        try:
            self.log_file = os.path.join(self.img_path, '1.log')
            with open(self.log_file) as fp:
                for l in fp.readlines():
                    segs = l.split(',')
                    name = segs[0].split(':')[1][:-9].strip()
                    inside_cost = float(segs[1].split(':')[1])
                    outside_cost = float(segs[2].split(':')[1])
                    self.img_infos[name] = (inside_cost, outside_cost)
            self.calcExcellentRate()
        except Exception as e:
            self.lbInfo.setText(e)

    def prev(self):
        if self.curr_img_id > 0:
            self.curr_img_id -= 1
        else:
            self.curr_img_id = len(self.img_names) - 1
        self.view()

    def next(self):
        if self.curr_img_id < len(self.img_names) - 1:
            self.curr_img_id += 1
        else:
            self.curr_img_id = 0
        self.view()

if __name__=='__main__':
    app=QApplication(sys.argv)
    w=win()
    w.show()
    sys.exit(app.exec_())
